﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace IS2
{
    public partial class FactorDeRiesgoForm : Form
    {
        //List<FactorDeRiesgo> listaFactores;
        AutoCompleteStringCollection listaFactores = new AutoCompleteStringCollection();
        AreaDeRiesgoForm parentForm;
        
        FactorDeRiesgo factor = null;

        public FactorDeRiesgoForm(AreaDeRiesgoForm f)
        {
            InitializeComponent();
            cargarDatos();
            textBoxBuscar.AutoCompleteSource = AutoCompleteSource.CustomSource;
            textBoxBuscar.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            textBoxBuscar.AutoCompleteCustomSource = listaFactores;
            parentForm = f;
            

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void cargarDatos()
        {
            using (Model1Container ctx = new Model1Container())
            {
                var lista = (from f in ctx.FactorDeRiesgoSet
                                 select f.Nombre);
                foreach (string s in lista)
                {
                    listaFactores.Add(s);
                }

            }
        }

        private void textBoxBuscar_TextChanged(object sender, EventArgs e)
        {
            factor = null;
            
            using (Model1Container ctx = new Model1Container())
            {
                factor = (from f in ctx.FactorDeRiesgoSet
                             where f.Nombre == textBoxBuscar.Text
                             select f).SingleOrDefault();
                if (factor != null)
                {
                    textBoxIdentificador.Text = factor.Id.ToString();
                    textBoxNombre.Text = factor.Nombre;
                    textBoxDesc.Text = factor.Descripcion;

                  
                }


            }
           
           

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (factor != null)
            {
                using (Model1Container ctx = new Model1Container())
                {
                   

                    FactorConMonitoreo fm = new FactorConMonitoreo();
               
                    fm.Monitoreado = false;
                    fm.FactorDeRiesgo = factor;
                    
               
                   
                    parentForm.nuevaArea.ListaFactores.Add(fm);
                    
                }
            }
            this.Dispose();

        }


    }
}
